<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar @click-left="$router.back()" class="page-nav-bar" left-arrow title="黑马头条1"></van-nav-bar>====
    <!-- /导航栏 -->
    <div class="main-wrap">
      <img
        @click="pimg"
        style="width:100px;
      height:100px"
        src="https://img.yzcdn.cn/vant/apple-1.jpg"
        alt=""
      >
      <!-- 加载中 -->
      <div v-if="loading" class="loading-wrap">
        <van-loading color="#3296fa" vertical>加载中</van-loading>
      </div>
      <!-- /加载中 -->
      <!-- 加载完成-文章详情 -->
      <div v-else-if="articleInfo.title" class="article-detail">
        <!-- 文章标题 -->
        <h1 class="article-title">{{articleInfo.title}}</h1>
        <!-- /文章标题 -->
        <!-- 用户信息 -->
        <van-cell class="user-info" center :border="false">
          <!-- <van-image
            class="avatar"
            slot="icon"
            round
            fit="cover"
            src="https://img.yzcdn.cn/vant/cat.jpeg"
          />-->
          <van-image class="avatar" slot="icon" round fit="cover" :src="articleInfo.aut_photo"/>
          <div slot="title" class="user-name">{{articleInfo.aut_name}}</div>
          <div slot="label" class="publish-date">{{articleInfo.pubdate|format}}</div>
          {{articleInfo.aut_id}}==={{articleInfo.is_followed}}
          <!-- <van-button
            v-if="articleInfo.is_followed"
            class="follow-btn"
            type="info"
            color="#3296fa"
            round
            size="small"
            icon="plus"
            :loading='btnloading'
            @click="isfollowfunc"
          >关注</van-button>
          <van-button :loading='btnloading' @click="isfollowfunc" v-else class="follow-btn" round size="small">已关注</van-button>-->

          <!-- 封装好的 关注组件 -->
          <!-- <follow-user @ff='articleInfo.is_followed=$event' :is_followed='articleInfo.is_followed' :aut_id='articleInfo.aut_id'></follow-user> -->
          <!-- 
              我们自定义一个v-model
              v-model======其实就是input+value的一起使用

              一个组件只能写一个v-model      
          -->
          <follow-user v-model="articleInfo.is_followed" :aut_id="articleInfo.aut_id"></follow-user>
          <!-- 封装好的 关注组件 -->
        </van-cell>
        <!-- /用户信息 -->
        <!-- 文章内容 -->
        <div class="article-content" v-html="articleInfo.content"></div>
        <van-divider>正文结束</van-divider>
        <!-- 评论列表展示 -->
        <comment-list
          @replyBtn="replyBtn"
          @totalCount="totalCount"
          :artId="artId"
          :list="newObjComment"
        ></comment-list>
        <!-- 评论列表展示 -->
        <!-- 底部区域 -->
        <div class="article-bottom">
          <van-button @click="show=true" class="comment-btn" type="default" round size="small">写评论</van-button>
          <van-icon name="comment-o" :info="total" color="#777"/>
          <!-- <van-icon v-if='starFlag'  color="#777" name="star-o"/>
          <van-icon v-else  color="#777" name="star-o"/>-->

          <conllect
            :artId="artId"
            :is_collected="articleInfo.is_collected"
            @coll="articleInfo.is_collected=$event"
          ></conllect>
          <!-- <van-icon color="#777" name="good-job-o"/> -->
          <!-- 点赞 -->
          <like v-model="articleInfo.attitude" :artId="artId"></like>
          <!-- 点赞 -->
          <van-icon name="share" color="#777777"></van-icon>
        </div>
        <!-- /底部区域 -->
      </div>
      <!-- /加载完成-文章详情 -->
      <!-- 加载失败：404 -->
      <div v-else-if="notfind" class="error-wrap">
        <van-icon name="failure"/>
        <p class="text">该资源不存在或已删除====404！</p>
      </div>
      <!-- /加载失败：404 -->
      <!-- 加载失败：其它未知错误（例如网络原因或服务端异常） -->
      <div v-else class="error-wrap">
        <van-icon name="failure"/>
        <p class="text">内容加载失败！</p>
        <van-button class="retry-btn">点击重试</van-button>
      </div>
      <!-- /加载失败：其它未知错误（例如网络原因或服务端异常） -->
      <!-- 弹出层 -->
      <van-popup v-model="show" position="bottom" :style="{ height: '30%' }">
        <post @shouClose="shouClose" :artId="artId"></post>
        <!-- <van-field
          v-model="message"
          rows="2"
          autosize
          type="textarea"
          maxlength="50"
          placeholder="请输入留言"
          show-word-limit
        />-->
      </van-popup>
      <!-- 弹出层 -->
      <!-- 回复评论 -->
      <van-popup v-model="showComment" position="bottom" :style="{ height: '80%' }">
        <!-- <comment-item :comment="comment"></comment-item> -->
        <comment-replay v-if="showComment" :comment="comment"></comment-replay>
      </van-popup>
    </div>
  </div>
</template>
           
<script>
import {
  getArticles_api,
  followings_api,
  followingsDel_api
} from '@/api/article.js'

import { ImagePreview } from 'vant'

import followUser from '@/components/followed'
import conllect from '@/components/conllect'
import like from '@/components/like'
import post from '@/components/post'
import commentList from '@/components/comment-list'

import commentReplay from '@/components/comment-replay'

// import jsonBig from 'json-bigint'
export default {
  provide: function() {
    return {
      aId: this.artId
    }
  },
  components: {
    followUser,
    conllect,
    like,
    post,
    commentList,
    // commentItem,
    commentReplay
  },
  //props:['artId'],
  props: {
    artId: {
      type: [String, Number, Object],
      required: true
    }
  },
  data() {
    return {
      // artId:this.$route.params.artId,
      articleInfo: {},
      loading: false,
      notfind: false,
      show: false,
      total: 0,
      newObjComment: [],
      /* 回复评论 */
      showComment: false,
      comment: []
      // btnloading: false,
    }
  },
  created() {
    // console.log(this,7788);
    this.getArticleDetail()

    //  演示大数字问题
    const jsonStr = '{ "art_id": 1245953273786007552 }'
    //  console.log(JSON.parse(jsonStr),8888)
    //  console.log(jsonBig.parse(jsonStr).art_id.toString(),9999)
    //  console.log(JSON.stringify(jsonBig.parse(jsonStr)),9999)
  },
  methods: {
    // 评论数的获取
    totalCount(cc) {
      console.log(11111111111)
      this.total = cc
    },
    fffunc($event) {
      console.log($event, 777)
      this.articleInfo.is_followed = $event
    },
    //  关注
    // async isfollowfunc() {
    //   this.btnloading=true
    //   // this.isfollow=!this.isfollow

    //   if (this.articleInfo.is_followed) {
    //     const { data: res } = await followingsDel_api(this.articleInfo.aut_id)
    //     console.log(res, 444)
    //   } else {
    //     //   发送请求
    //     const { data: res } = await followings_api({
    //       target: this.articleInfo.aut_id
    //     })
    //     //console.log(res.data.target.toString(), 6666)
    //   }
    //   this.articleInfo.is_followed = !this.articleInfo.is_followed
    //    this.btnloading=false
    //   //  接口联调
    // },
    //  图片预览
    pimg() {
      ImagePreview({
        images: [
          'https://img.yzcdn.cn/vant/apple-1.jpg',
          'http://static.zybuluo.com/buptzym/hzr6wujaewov0shnvn5ke3qc/image_1bg2h688f1g3msap1v3c1bf11hho19.png',
          'https://img.yzcdn.cn/vant/apple-1.jpg'
        ],
        startPosition: 0
      })
    },
    async getArticleDetail() {
      this.loading = true
      try {
        const { data: res } = await getArticles_api(this.artId)
        console.log(res, 444333)
        this.articleInfo = res.data
      } catch (error) {
        console.log(error.response.status, 888)
        if (error.response.status == 404) {
          this.notfind = true
        }
      }
      this.loading = false
    },

    //  将post组件中添加评论的数据传给index.vue【父组件】
    shouClose(result) {
      //  关闭对话框  +  接受添加的评论
      this.show = false
      this.newObjComment.unshift(result)
      //this.newObjComment=[
    },

    /* 回复评论对话框 */
    replyBtn(comment) {
      console.log(comment, 77777)
      this.showComment = true

      this.comment = comment
    }
  }
}
</script>
<style scoped lang="less">
.article-container {
  .main-wrap {
    position: fixed;
    left: 0;
    right: 0;
    top: 92px;
    bottom: 88px;
    overflow-y: scroll;
    background-color: #fff;
  }
  .article-detail {
    .article-title {
      font-size: 40px;
      padding: 50px 32px;
      margin: 0;
      color: #3a3a3a;
    }

    .user-info {
      padding: 0 32px;
      .avatar {
        width: 70px;
        height: 70px;
        margin-right: 17px;
      }
      .van-cell__label {
        margin-top: 0;
      }
      .user-name {
        font-size: 24px;
        color: #3a3a3a;
      }
      .publish-date {
        font-size: 23px;
        color: #b7b7b7;
      }
      .follow-btn {
        width: 170px;
        height: 58px;
      }
    }

    .article-content {
      padding: 55px 32px;
      /deep/ p {
        text-align: justify;
      }
    }
  }

  .loading-wrap {
    padding: 200px 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #fff;
  }

  .error-wrap {
    padding: 200px 32px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background-color: #fff;
    .van-icon {
      font-size: 122px;
      color: #b4b4b4;
    }
    .text {
      font-size: 30px;
      color: #666666;
      margin: 33px 0 46px;
    }
    .retry-btn {
      width: 280px;
      height: 70px;
      line-height: 70px;
      border: 1px solid #c3c3c3;
      font-size: 30px;
      color: #666666;
    }
  }

  .article-bottom {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    justify-content: space-around;
    align-items: center;
    box-sizing: border-box;
    height: 88px;
    border-top: 1px solid #d8d8d8;
    background-color: #fff;
    .comment-btn {
      width: 282px;
      height: 46px;
      border: 2px solid #eeeeee;
      font-size: 30px;
      line-height: 46px;
      color: #a7a7a7;
    }
    .van-icon {
      font-size: 40px;
      .van-info {
        font-size: 16px;
        background-color: #e22829;
      }
    }
  }
}
</style>